package com.ssyc.student.activity;

import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.OneKeyShareCallback;
import com.ssyc.common.manager.ShareManager;
import com.ssyc.common.manager.StateBarManager;
import com.ssyc.common.utils.WebViewUtils;
import com.ssyc.student.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;

/**
 * 学生练习题库
 * Created by Kevin on 2018/8/10.
 */

public class StudentPracticeWorkActivity extends BaseActivity {

    private WebView webView;
    private String examId;
    private String[] split;
    private String[] times;
    private String[] marks;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.base_activity_tk;
    }

    @Override
    protected void init() {

        showContent();

        //配置状态栏颜色
        initStateColor();

        initIntent();

        initView();

        setWebView();
    }

    /**
     * 接收参数
     */
    private void initIntent() {
        examId = getIntent().getStringExtra("examId");

    }


    /**
     * 配置状态栏颜色的方法
     */
    private void initStateColor() {
        //设置6.0以后沉浸状体栏的问题
        StateBarManager.setBarWithUltimateBar(this, getResources().getColor(com.ssyc.common.R.color.tkColor),
                getResources().getColor(com.ssyc.common.R.color.nav_color));
    }

    /**
     * 初始化视图
     */
    private void initView() {
        webView = findView(R.id.webView);
    }

    /**
     * 设置Webview
     */
    private void setWebView() {
        WebViewUtils.setWebview(this, webView);

        webView.setWebViewClient(new myWebViewClient(

        ));
        webView.addJavascriptInterface(new WebAppInterface(this), "Android");
        //学生练习题库
        webView.loadUrl("https://moregolden.com:36357/tkNewPageTest/test4.html?appType=2&role=0&examId=" + examId);
    }

    public class myWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            webView.loadUrl("javascript:alertName('" + AccountUtils.getAccount(StudentPracticeWorkActivity.this)
                    + "','" + AccountUtils.getName(StudentPracticeWorkActivity.this) + "')");
        }
    }


    public class WebAppInterface {
        Context mContext;

        /**
         * Instantiate the interface and set the context
         */
        WebAppInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void showToast(String toast) {
            finish();
        }

        @JavascriptInterface
        public void finishActivity() {
            finish();
        }

        @JavascriptInterface
        public void IntentButtonActivity() {
            finish();
        }

        @JavascriptInterface
        public void ExitLogin() {
            finish();
        }

        // 传递参数
        @JavascriptInterface
        public String getParam() {
            String param;

            param = AccountUtils.getAccount(StudentPracticeWorkActivity.this);
            Log.e("test", "param=" + param);
            return param;
        }

        @JavascriptInterface
        public void shareResults(String scoreInfo) {
            //moduleType=1&userType=0&time=41&mark=0&num=18271261779

            split = scoreInfo.split("&");
            times = split[2].split("=");
            marks = split[3].split("=");

            //新地址
            String shareUrl = HttpAdress.UPDATE_URL + "JPXB/sharePage/pages/JPXGN_JPXB/shareMark/shareMark.html?" + scoreInfo + "&exam_type=3&appType=2";

            //图标
            String imgUrl = "http://139.129.57.83:8080/Appandroid/jpxblogo.png";

            //分享标题
            String title = AccountUtils.getName(StudentPracticeWorkActivity.this) + "在题库环节花费" + times[1] + "秒正确率为" + marks[1] + "%";
            String content = "想展现你的实力吗?来这里比拼一下吧！";

            ShareManager.newInstance().showOneShare(StudentPracticeWorkActivity.this, title, shareUrl,
                    content, imgUrl, shareUrl, new OneKeyShareCallback() {
                        @Override
                        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                            Log.i("test", "分享成功");
                        }

                        @Override
                        public void onError(Platform platform, int i, Throwable throwable) {
                            Log.i("test", "分享失败");
                        }

                        @Override
                        public void onCancel(Platform platform, int i) {
                            Log.i("test", "取消分享");
                        }
                    });
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.loadUrl("about:blank");
        webView.stopLoading();
        webView.setWebChromeClient(null);
        webView.setWebViewClient(null);
        webView.destroy();
        webView = null;
    }
}
